package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.dubbo.mappers.ReportMapper;
import com.tanhua.model.domain.Report;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author pc
 * @date 2021/11/28
 */
@DubboService
public class ReportApiImpl implements ReportApi {
    @Autowired
    private ReportMapper reportMapper;

    /**
     * 保存报告
     * @param report
     * @return
     */
    @Override
    public Integer save(Report report) {
        Integer userId = report.getUserId();
        Integer questionnaireId = report.getQuestionnaireId();
        LambdaQueryWrapper<Report> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Report::getUserId, userId);
        lqw.eq(Report::getQuestionnaireId, questionnaireId);
        reportMapper.delete(lqw);
        reportMapper.insert(report);
        return report.getId();
    }

    /**
     * 获取报告id
     * @param questionnaireId
     * @param userId
     * @return
     */
    @Override
    public Integer find(Integer questionnaireId, Long userId) {
        LambdaQueryWrapper<Report> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Report::getUserId, Integer.valueOf(userId.toString()));
        lqw.eq(Report::getQuestionnaireId, questionnaireId);
        Report report = reportMapper.selectOne(lqw);
        if (report == null) {
            return null;
        }
        return report.getId();
    }

    /**
     * 查询问卷报告
     * @param reportId
     * @return
     */
    @Override
    public Report look(Integer reportId) {
        return reportMapper.selectById(reportId);
    }

    /**
     * 查询相似用户id
     * @param dimensions
     * @return
     */
    @Override
    public List<Integer> findIds(Integer dimensions) {
        LambdaQueryWrapper<Report> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Report::getDimensions, dimensions);
        List<Report> reports = reportMapper.selectList(lqw);
        return CollUtil.getFieldValues(reports, "userId", Integer.class);
    }


}
